package com.pinc.exam;

import android.content.Context;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.ViewFlipper;

public abstract class FlipActivity extends WidgetActivity implements OnGestureListener{
	protected ViewFlipper flipper;
	protected LinearLayout[] mLayout;
	protected int cur_qstn;
	protected GestureDetector detector = new GestureDetector(this);

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return this.detector.onTouchEvent(event);
	}

	@Override
	protected void initWidget() {
		super.initWidget();

		flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper01);

		mLayout = new LinearLayout[GParam.PAGE_COUNT];

		LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		for ( int i=0; i < GParam.PAGE_COUNT; i++)
		{
			mLayout[i] = (LinearLayout) inflater.inflate(R.layout.flip_page, new LinearLayout(this));
			flipper.addView(mLayout[i]);
		}
	}

	protected void showNext()
	{
		flipper.setInAnimation(AnimationUtils.loadAnimation(FlipActivity.this, R.anim.left_in));
		flipper.setOutAnimation(AnimationUtils.loadAnimation(FlipActivity.this, R.anim.left_out));
		flipper.showNext();
		cur_qstn = getNextNO(cur_qstn, getQuestionCnt());
		fillPage((LinearLayout)flipper.getChildAt(getNextNO(flipper.getDisplayedChild(), GParam.PAGE_COUNT)), getNextNO(cur_qstn, getQuestionCnt()));
		postHandle(cur_qstn);
	}

	protected void showPrev()
	{
		flipper.setInAnimation(AnimationUtils.loadAnimation(FlipActivity.this, R.anim.right_in));
		flipper.setOutAnimation(AnimationUtils.loadAnimation(FlipActivity.this, R.anim.right_out));
		flipper.showPrevious();
		cur_qstn = getPrevNO(cur_qstn, getQuestionCnt());
		fillPage((LinearLayout)flipper.getChildAt(getPrevNO(flipper.getDisplayedChild(), GParam.PAGE_COUNT)), getPrevNO(cur_qstn, getQuestionCnt()));
		postHandle(cur_qstn);
	}

	protected void showQuestion()
	{
		fillPage(mLayout[0], cur_qstn);
		postHandle(cur_qstn);
		fillPage(mLayout[1], getNextNO(cur_qstn, getQuestionCnt()));
		fillPage(mLayout[2], getPrevNO(cur_qstn, getQuestionCnt()));
	}

	protected void fillPage(LinearLayout ll, final int qstn_no)
	{
		TextView tv_q = (TextView) ll.findViewById(R.id.textView_Q);
		ImageView iv_q = (ImageView) ll.findViewById(R.id.imageView_Q);
		TextView tv_qh = (TextView) ll.findViewById(R.id.textView_Qh);
		TextView tv_ctg = (TextView) ll.findViewById(R.id.textView_Ctg);
		LinearLayout[] ll_sel = new LinearLayout[GParam.SELECT_MAX];
		ll_sel[0] = (LinearLayout) ll.findViewById(R.id.linearLayout_A);
		ll_sel[1] = (LinearLayout) ll.findViewById(R.id.linearLayout_B);
		ll_sel[2] = (LinearLayout) ll.findViewById(R.id.linearLayout_C);
		ll_sel[3] = (LinearLayout) ll.findViewById(R.id.linearLayout_D);
		ll_sel[4] = (LinearLayout) ll.findViewById(R.id.linearLayout_E);

		TextView[] tv_sel = new TextView[GParam.SELECT_MAX];
		tv_sel[0] = (TextView) ll.findViewById(R.id.textView_A);
		tv_sel[1] = (TextView) ll.findViewById(R.id.textView_B);
		tv_sel[2] = (TextView) ll.findViewById(R.id.textView_C);
		tv_sel[3] = (TextView) ll.findViewById(R.id.textView_D);
		tv_sel[4] = (TextView) ll.findViewById(R.id.textView_E);
		tv_qh.setText(String.format(" %d. ", qstn_no+1));
		tv_q.setText(getQuestion(qstn_no).replace("\\n", "\n"));
		if ( getImageIndex(qstn_no) < 0)
			iv_q.setVisibility(View.GONE);
		else
		{
			iv_q.setVisibility(View.VISIBLE);
			iv_q.setImageResource(GParam.ARR_IMGID[getImageIndex(qstn_no)]);
		}
		tv_ctg.setText(getCategoryName(qstn_no));
		String[] slct = getSelections(qstn_no); 
		for ( int i = 0; i< slct.length; i++)
		{
			ll_sel[i].setVisibility(View.VISIBLE);
			tv_sel[i].setText(slct[i]);
		}
		for ( int i = slct.length; i < GParam.SELECT_MAX; i++)
		{
			ll_sel[i].setVisibility(View.GONE);
		}
		ScrollView sv = (ScrollView)ll.findViewById(R.id.scrollView1);
		sv.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return FlipActivity.this.onTouchEvent(event);
			}
		});
		markSelection(tv_sel, qstn_no);
	}

	protected TextView[] getCurPageTextViews()
	{
		TextView[] tv_sel = new TextView[GParam.SELECT_MAX];
		tv_sel[0] = (TextView) flipper.getCurrentView().findViewById(R.id.textView_A);
		tv_sel[1] = (TextView) flipper.getCurrentView().findViewById(R.id.textView_B);
		tv_sel[2] = (TextView) flipper.getCurrentView().findViewById(R.id.textView_C);
		tv_sel[3] = (TextView) flipper.getCurrentView().findViewById(R.id.textView_D);
		tv_sel[4] = (TextView) flipper.getCurrentView().findViewById(R.id.textView_E);
		return tv_sel;
	}

	protected void postHandle(int no){};
	protected String getCategoryName(int qstn_no){ return "";};

	protected abstract String getQuestion(int qstn_no);
	protected abstract int getImageIndex(int qstn_no);
	protected abstract String[] getSelections(int qstn_no);
	protected abstract void markSelection(TextView[] tv_sel, int qstn_no);
	protected abstract int getQuestionCnt();

	private static int getNextNO(int n, int total)
	{
		return (n+1)%total;
	}

	private static int getPrevNO(int n, int total)
	{
		return (n-1+total)%total;
	}

	@Override
	public boolean onDown(MotionEvent e) {
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,	float velocityY) {
		if (e1.getX() - e2.getX() > 0 && Math.abs(e1.getY() - e2.getY()) < Math.abs(e1.getX() - e2.getX()))
		{
			if ( cur_qstn < getQuestionCnt() - 1)
				showNext();
			return true;
		} else if (e1.getX() - e2.getX() < 0 && Math.abs(e1.getY() - e2.getY()) < Math.abs(e1.getX() - e2.getX()) )
		{
			if ( cur_qstn > 0)
				showPrev();
			return true;
		}
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}
}
